<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		//modell
		$this->load->model('Products_model');
		$this->load->model('Orderedcategories_model');
		
		$data['products_model'] = $this->Products_model;
		$data['categories'] = $this->Orderedcategories_model;
		$data['title'] = $this->Orderedcategories_model->getFirstCategoryName();
		$data['products'] = $this->Products_model->categorizedProducts(10);
		$data['data'] = $data;
		
		$this->load->view('admin/admin_view', $data);
	}
	
	public function category()
	{
		$this->load->model('Products_model');
		$data['products_model'] = $this->Products_model;
		
		//view
		$this->load->view('admin/header');
		$this->load->view('admin/categories', $data);
		$this->load->view('admin/addCategoryModal');
		$this->load->view('admin/editCategoryModal');
		$this->load->view('admin/confirmationModal', array('title' => 'Kategória törlése', 'text' => 'Biztosan törölni szeretné ezt a kategóriát?', 'url' => '/smaragd/admin/removecategory'));
		$this->load->view('admin/categoryScripts');
	}
	
	public function updatecategory() {
		$data = array('category' => $this->input->post('name'));
		
		$this->db->where('id', $this->input->post('id'));
		$this->db->update('categories', $data); 
		
		$url = base_url().'admin/category';
		redirect($url);
	}
	/*
	private function removeCategoryAndItsChilden($id, $model) {
		$childern = $model->getCategory($id)->subCategories;
    
    	foreach($childern as $child) {
    		$this->removeCategoryAndItsChilden($child->id, $model);
    	}
    	
    	$this->db->delete('categories', array('id' => $id));		
	}
	*/
	public function removeproduct($id) 
	{
		$this->load->model('Products_model');
		$this->Products_model->removeProduct($id);
		/*$productID = $this->input->post('id');
		$this->db->delete('products', array('id' => $productID));
		
		$url = base_url().'admin';
		redirect($url);
		*/
	}
	
	public function editproduct() {
		$this->load->model('Products_model');
		
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$description = $this->input->post('description');
		$netto_price = $this->input->post('netto_price');
		$price = $this->input->post('price');
		$profit_key = $this->input->post('profit_key');
		$category_id = $this->input->post('category_id');
		
		$this->Products_model->editProduct($id, $name, $description, $netto_price, $price, $profit_key, $category_id);
		/*
		$data = array(
            'name' => $this->input->post('name'),
            'description' => $this->input->post('description'),
			'netto_price' => $this->input->post('netto_price'),
			'price' => $this->input->post('price'),
			'profit_key' => $this->input->post('profit_key'),
			'category_id' => $this->input->post('category_id')
        );

		$this->db->where('id', $this->input->post('id'));
		$this->db->update('products', $data); 
		*/
		
		//$url = base_url().'admin';
		//redirect($url);
	}
	
	public function updateisfeatured() {
		$data = array(
            'isFeatured' => $this->input->post('isFeatured')
        );

		$this->db->where('id', $this->input->post('id'));
		$this->db->update('products', $data); 
		
		echo "KAKA" . $this->input->post('isFeatured') . " " . $this->input->post('id');
		
	}
	
	/*
	public function addcategory() {
		if($this->input->post('parent_id') == "NULL") 
		{
			$data = array(
	            'category' => $this->input->post('category')
	        );	
		} else {
			$data = array(
	            'category' => $this->input->post('category'),
				'parent_id' => $this->input->post('parent_id')
        	);
		}
		
        $this->db->insert('categories', $data); 
        $url = base_url().'admin/category';
		redirect($url);
	}*/
	
	public function addproduct() 
	{
		$config['upload_path'] = './resources/products/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '30000';
		$config['max_width']  = '30024';
		$config['max_height']  = '36800';

		$this->load->library('upload', $config);

		if (!$this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			$this->upload->display_errors('<p>', '</p>');
			$this->load->view('admin/upload_error', $error);
		}
		else
		{
			$upload_data = $this->upload->data(); 
		
			$data = array(
	            'name' => $this->input->post('name'),
	            'description' => $this->input->post('description'),
				'netto_price' => $this->input->post('netto_price'),
				'price' => $this->input->post('price'),
				'profit_key' => $this->input->post('profit_key'),
				'category_id' => $this->input->post('category_id'),
				'image' => $upload_data['file_name']
	        );
	
			$this->db->insert('products', $data); 
			
			$url = base_url().'admin';
			redirect($url);
		}
	}
	
	public function getproductsbycategory() {
		$category_id = $this->input->post('category_id');
		$name = $this->input->post('name');
		$this->load->model('Products_model');
		$this->load->model('Orderedcategories_model');
		
		$products = $this->Products_model->categorizedProducts($category_id);
		
		$data['title'] = $name;
		$data['products'] = $products;
		$data['data'] = $data;
		
		$this->load->view('admin/products_view', $data);
	}
	
	public function addcategory() {
		$this->load->model('Orderedcategories_model');
		$categories = $this->Orderedcategories_model;
		
		$category_id = $this->input->post('category_id');
		$name = $this->input->post('name');
		$this->Orderedcategories_model->addCategory($category_id, $name);
		
		$data['categories'] = $categories;
		$data['data'] = $data;
		$this->load->view('admin/categories', $data);
	}
	
	public function editcategory() {
		$this->load->model('Orderedcategories_model');
		$categories = $this->Orderedcategories_model;
		
		$category_id = $this->input->post('category_id');
		$name = $this->input->post('name');
		$categories->editCategory($category_id, $name);
		
		$data['categories'] = $categories;
		$data['data'] = $data;
		$this->load->view('admin/categories', $data);
	}
	
	public function removecategory() {
		$this->load->model('Orderedcategories_model');
		$this->load->model('Products_model');
		
		$categories = $this->Orderedcategories_model;
		$products = $this->Products_model;
		$category_id = $this->input->post('category_id');
		
		$products->clearProductCategories($category_id);
		$categories->removeCategory($category_id);
		
		$data['categories'] = $categories;
		$data['data'] = $data;
		$this->load->view('admin/categories', $data);
	}
	
	public function movecategoryup() {
		$this->load->model('Orderedcategories_model');
		$categories = $this->Orderedcategories_model;
		$category_id = $this->input->post('category_id');
		
		$categories->moveCategoryUp($category_id);

		$data['categories'] = $categories;
		$data['data'] = $data;
		$this->load->view('admin/categories', $data);
	}
	
	public function movecategorydown() {
		$this->load->model('Orderedcategories_model');
		$categories = $this->Orderedcategories_model;
		$category_id = $this->input->post('category_id');
		
		$categories->moveCategoryDown($category_id);

		$data['categories'] = $categories;
		$data['data'] = $data;
		$this->load->view('admin/categories', $data);
	}
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */